﻿package soundCom 
{
	import debugs.Debug;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import media.sound;
	import net.netvars;
	import media.fixSound;
	import media.soundItem;
	import flash.events.Event;
	import src.libCom.FixLib
	/**
	 * ...
	 * @author keys_star@163.com
	 */
	public class QA extends EventDispatcher
	{
		private var soundArr:Array;
		private var si:soundItem;
		private var timers:Timer;
		private var LIB:FixLib;
		
		private var MID:uint = 0;
		private var MLEN:uint = 0;
		
		private var AID:uint = 0;
		private var ALEN:uint = 0;
		
		private var AID1:uint = 0;
		
		public var TYPE:String = "";
		public var ID:uint = 0;
		
		public function QA() 
		{
			initTimer();
		}
		public function init() {
			soundArr = new Array;
			//var moneySound:Array = getMoneyItem();
			var titleSound:Array = getTitle();
			var mobileSound:Array = getMobile();
			var ends:Array = getEndS();
			//var awardSound:Array = getAward();
			AID = soundArr.length;
			//ALEN = moneySound.length;
			
			//soundArr = soundArr.concat(moneySound);
			soundArr = soundArr.concat(titleSound);
			
			MLEN = mobileSound.length;
			MID = soundArr.length;
			soundArr = soundArr.concat(mobileSound);
			soundArr = soundArr.concat(ends);
			
			//soundArr = soundArr.concat(awardSound);
			
			AID1 = soundArr.length;
			//soundArr = soundArr.concat(moneySound);
			Debug.send(this+".soundArr: "+soundArr.join("]["));
		}
		private function getMoneyItem():Array {
			var u:String = "sound/";
			var m = Number(netvars.getVar("MONEY"));//钱数
			var sn = fixSound.getNumSound(m, "sound");
			var a:Array = [u + 5];
			a = a.concat(sn);
			return a;
		}
		private function getTitle():Array {
			var u:String = "sound/";
			var tid = Number(netvars.getVar("ID"));
			var s = "QALIB/" + "LIB" + tid + "/Q";
			//return [u+1,s];
			return [u+1];
		}
		private function getMobile():Array {
			var u:String = "sound/";
			var arr = netvars.getVar("PH") as Array;//电话
			var soItem = new Array;
			for (var i = 0; i < arr.length; i++ ) {
				soItem.push(u+"ph" + i);
			}
			return soItem;
		}
		private function getAward():Array {
			var u:String = "sound/";
			var s0:Array = [u + 4];
			return s0;
		}
		private function getEndS():Array {
			var u:String = "sound/";
			var s0:Array = [u + 2];
			return s0;
		}
		public function play() {
			TYPE = "";
			si = new soundItem;
			si.addEventListener(Event.SOUND_COMPLETE, scom);
			si.addEventListener(Event.CHANGE, schange);
			si.soundArr = soundArr;
			si.play();
		}
		private function schange(e:Event) {
			var sid = si.id;
			if(checkMoney(sid, AID)||checkMoney(sid, AID1)||checkMobile(sid)) {
				dispatchEvent(e);
			}
		}
		private function checkMoney(sid:uint,aid:uint) {
			if (sid >= aid && sid < aid+1) {
				TYPE = "MoneyPlay";
				return true;
			}else if (sid == ALEN + aid) {
				TYPE = "MoneyClose";
				return true;
			}
		}
		private function checkMobile(sid:uint) {
			if (sid >= MID && sid < MLEN + MID) {
				TYPE = "MobilePlay";
				ID = sid-MID;
				return true;
			}
		}
		private function scom(e:Event) {
			dispatchEvent(new Event(Event.COMPLETE));
			//timerStart();
		}
		public function stop() {
			Debug.send(this + ".all.stop");
			si.stop();
			timerStop();
		}
		public function timerStart() {
			initTimer();
			timers.start();
		}
		public function timerStop() {
			timers.reset();
		}
		public function initTimer() {
			timers = new Timer(3*1*1000,1);
			timers.addEventListener(TimerEvent.TIMER, tcom);
		}
		private function tcom(e:TimerEvent) {
			Debug.send(this + ".start");
			play();
		}
	}
}